$(function () {
    // 加载数据
    queryUserDetail();
})

var userDetail = "";
function queryUserDetail(){
    $request({
        url: '/user-center-oauth/oauthUser/detail',
        method: 'get'
    }, function (res) {
        userDetail = res;
        let avatar = 'http:139.198.15.29:40102/user-center-oauth/file/' + res.avatar
        if (res.avatar) {
            $('.box-editData .head-img img').attr('src', avatar)
        }
        $("#nickname").text(res.nickname);
        $("#nicknameUpdate").val(res.nickname);
        $("#phone").text(res.phone);
        $("#phoneHandle").text(res.phone == "" ? "去绑定" : "修改");
        $("#phoneTitle").text(res.phone == "" ? "手机号绑定" : "修改手机号");
        if(res.phone == "" || res.phone == null){
            $("#phoneOldDiv").hide();
        }else{
            $("#phoneOldDiv").show();
        }
        $("#email").text(res.email);
        $("#emailHandle").text(res.email == "" ? "去绑定" : "修改");
        $("#emailTitle").text(email == "" ? "绑定邮箱" : "修改邮箱");
        if(res.email == "" || res.email == null){
            $("#emailOldDiv").hide();
        }else{
            $("#emailOldDiv").show();
        }
        $("#password").text(res.ifpassword == 1 ? "******" : "");
        $("#passwordHandle").text(res.ifpassword == 1 ? "设置密码" : "修改密码");
        $("#bid").text(res.bid);
        if(res.bid == ""){
            $("#copgBidDiv").hide();
        }
        if(res.keyStore == ""){
            $("#typeDivWtg").show();
            $("#typeDivYtg").hide();
        }else{
            $("#typeDivWtg").hide();
            $("#typeDivYtg").show();
        }
    });
}

function validatePasswordOld(){
    var passwordOld = $("#passwordOld").val();
    if(passwordOld == ""){
        $("#passwordOldError").show();
        return false;
    }
    $("#passwordOldError").hide();
    return true;
}

function validatePasswordNew(){
    var passwordNew = $("#passwordNew").val();
    if(passwordNew == ""){
        $("#passwordNewError").text("请输入密码");
        $("#passwordNewError").show();
        return false;
    }
    var reg = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&.])[A-Za-z\d$@$!%*?&]{8,32}/;
    if(!reg.test(passwordNew)){
        $("#passwordNewError").text("密码提示：输入密码应当是8-32位字符，包含数字、小写、大写、特殊字符");
        $("#passwordNewError").show();
        return false;
    }
    $("#passwordNewError").hide();
    return true;
}

function updatePassword(){
    if(!validatePasswordOld()){
        return false;
    }
    if(!validatePasswordNew()){
        return false;
    }
    if(!validatePasswordSure()){
        return false;
    }
    var oldPassword = $("#passwordOld").val();
    var oldPasswordEn = encrypt(oldPassword, "e0da368a8fa3d9b3f691e82957ec5b75").toUpperCase();
    var newPassword = $("#passwordNew").val();
    var newPasswordEn = encrypt(newPassword, "e0da368a8fa3d9b3f691e82957ec5b75").toUpperCase();
    var data = {oldPassword: oldPasswordEn, newPassword: newPasswordEn};
    console.log(getUuid());
    $.ajax({
        url: '/user-center-oauth/oauthUser/password',
        type: "post",
        data: JSON.stringify(data),
        headers: {Authorization: "bearer " + localStorage.getItem("ACCESS_TOKEN"), check: localStorage.getItem("username"),
            Auth: getUuid(), 'Content-Type': 'application/json'},
        dataType: "json",
        success: function (res) {
            if (res != "" && res.code == 200) {
                alert("修改密码成功");
                window.location.href = "personalCenter.html";
            } else {
                alert(res.msg);
            }
        }
    });
}

function validatePasswordSure(){
    var passwordSure = $("#passwordSure").val();
    if(passwordSure == ""){
        $("#passwordSureError").text("请输入密码");
        $("#passwordSureError").show();
        return false;
    }
    var passwordNew = $("#passwordNew").val();
    if(passwordSure != passwordNew){
        $("#passwordSureError").text("密码和确认密码不一致");
        $("#passwordSureError").show();
        return false;
    }
    $("#passwordSureError").hide();
    return true;
}

function sendCheckCodePhoneOld() {
    var timer = setInterval(beginTime,1000);
    var countdown=60;
    function beginTime() {
        $("#checkCodePhoneOldBtn").text(countdown + "秒后获取");
        if (countdown == 1) {
            $("#checkCodePhoneOldBtn").text("获取验证码");
            clearInterval(timer);
            return false;
        } else {
            if (countdown == 60) {
                var phone = userDetail.phone;
                $.ajax({
                    url: '/user-center-oauth/oauthUser/sendSms?phone=' + phone,
                    type: "get",
                    dataType: "json",
                    headers: {Authorization: "bearer " + localStorage.getItem("ACCESS_TOKEN"), check: localStorage.getItem("username"),
                        Auth: getUuid()},
                    success: function (data) {
                        if (data != "" && data.code == 200) {
                            alert("验证码发送成功");
                        } else {
                            alert(data.msg);
                            $("#checkCodePhoneOldBtn").text("获取验证码");
                            //进度百分百后，清除掉定时器
                            clearInterval(timer);
                        }
                    }
                });
            }
            countdown--;
        }
    }
}

function sendCheckCodePhone() {
    if(!validatePhone()){
        return false;
    }
    var timer = setInterval(beginTime,1000);
    var countdown=60;
    function beginTime() {
        $("#checkCodePhoneBtn").text(countdown + "秒后获取");
        if (countdown == 1) {
            $("#checkCodePhoneBtn").text("获取验证码");
            clearInterval(timer);
            return false;
        } else {
            if (countdown == 60) {
                var phone = $("#phoneVaule").val();
                $.ajax({
                    url: '/user-center-oauth/oauthUser/sendSms?phone=' + phone,
                    type: "get",
                    dataType: "json",
                    headers: {Authorization: "bearer " + localStorage.getItem("ACCESS_TOKEN"), check: localStorage.getItem("username"),
                        Auth: getUuid()},
                    success: function (data) {
                        if (data != "" && data.code == 200) {
                            alert("验证码发送成功");
                        } else {
                            alert(data.msg);
                            $("#checkCodePhoneBtn").text("获取验证码");
                            //进度百分百后，清除掉定时器
                            clearInterval(timer);
                        }
                    }
                });
            }
            countdown--;
        }
    }
}

function updatePhone(){
    if(userDetail.phone != "" && userDetail.phone != null && !validateCheckCodePhoneOld()){
        return false;
    }
    if(!validatePhone()){
        return false;
    }
    if(!validateCheckCodePhone()){
        return false;
    }
    var data = {oldPhoneCaptcha: $("#checkCodePhoneOld").val(), newPhone: $("#phoneVaule").val(), newPhoneCaptcha: $("#checkCodePhone").val()};
    $.ajax({
        url: '/user-center-oauth/oauthUser/updatePhone',
        type: "post",
        dataType: "json",
        data: JSON.stringify(data),
        headers: {Authorization: "bearer " + localStorage.getItem("ACCESS_TOKEN"), check: localStorage.getItem("username"),
            Auth: getUuid(), 'Content-Type': 'application/json'},
        success: function (data) {
            if (data != "" && data.code == 200) {
                alert("操作成功");
                window.location.href = "personalCenter.html";
            } else {
                alert(data.msg);
            }
        }
    });
}

function validateCheckCodePhoneOld(){
    var checkCode = $("#checkCodePhoneOld").val();
    if(checkCode == ""){
        $("#checkCodePhoneOldError").show();
        return false;
    }
    $("#checkCodePhoneOldError").hide();
    return true;
}

function validateCheckCodePhone(){
    var checkCode = $("#checkCodePhone").val();
    if(checkCode == ""){
        $("#checkCodePhoneError").show();
        return false;
    }
    $("#checkCodePhoneError").hide();
    return true;
}

function validatePhone(){
    var phone = $("#phoneVaule").val();
    if(phone == ""){
        $("#phoneError").text("请输入您的新手机号!");
        $("#phoneError").show();
        return false;
    }
    var reg = /^1\d{10}$/;
    if(!reg.test(phone) && !reg.test(phone)){
        $("#phoneError").text("请输入正确手机号码!");
        $("#phoneError").show();
        return false;
    }
    $("#phoneError").hide();
    return true;
}

function validateEmail() {
    var email = $("#emailValue").val();
    if(email == ""){
        $("#emailError").text("请输入需要绑定的邮箱!");
        $("#emailError").show();
        return false;
    }
    var reg = /^\w+[@]\w{2,5}([.]\w{2,3}){1,3}$/i;
    if(!reg.test(email) && !reg.test(email)){
        $("#emailError").text("请输入正确邮箱!");
        $("#emailError").show();
        return false;
    }
    $("#emailError").hide();
    return true;
}

function sendCheckCodeEmail() {
    if(!validateEmail()){
        return false;
    }
    var timer = setInterval(beginTime,1000);
    var countdown=60;
    function beginTime() {
        $("#checkCodeEmailBtn").text(countdown + "秒后获取");
        if (countdown == 1) {
            $("#checkCodeEmailBtn").text("获取验证码");
            clearInterval(timer);
            return false;
        } else {
            if (countdown == 60) {
                var email = $("#emailValue").val();
                $.ajax({
                    url: '/user-center-oauth/oauthUser/sendEmail?email=' + email,
                    type: "get",
                    dataType: "json",
                    headers: {Authorization: "bearer " + localStorage.getItem("ACCESS_TOKEN"), check: localStorage.getItem("username"),
                        Auth: getUuid(), 'Content-Type': 'application/json'},
                    success: function (data) {
                        if (data != "" && data.code == 200) {
                            alert("验证码发送成功");
                        } else {
                            alert(data.msg);
                            $("#checkCodeEmailBtn").text("获取验证码");
                        }
                    }
                });
            }
            countdown--;
        }
    }
}

function validateCheckCodeEmailOld(){
    var checkCodeEmailOld = $("#checkCodeEmailOld").val();
    if(checkCodeEmailOld == ""){
        $("#checkCodeEmailOldError").text("请输入当前邮箱收到的验证码");
        $("#checkCodeEmailOldError").show();
        return false;
    }
    $("#checkCodeEmailOldError").hide();
    return true;
}

function validateCheckCodeEmail(){
    var checkCodeEmail = $("#checkCodeEmail").val();
    if(checkCodeEmail == ""){
        $("#checkCodeEmailError").text("请输入验证码");
        $("#checkCodeEmailError").show();
        return false;
    }
    $("#checkCodeEmailError").hide();
    return true;
}

function sendCheckCodeEmailOld() {
    if(userDetail.email == "" || userDetail.email == null){
        alert("当前没有绑定邮箱");
        return false;
    }
    var timer = setInterval(beginTime,1000);
    var countdown=60;
    function beginTime() {
        $("#checkCodeEmailOldBtn").text(countdown + "秒后获取");
        if (countdown == 1) {
            $("#checkCodeEmailOldBtn").text("获取验证码");
            clearInterval(timer);
            return false;
        } else {
            if (countdown == 60) {
                var email = userDetail.email;
                $.ajax({
                    url: '/user-center-oauth/oauthUser/sendEmail?email=' + email,
                    type: "get",
                    dataType: "json",
                    headers: {Authorization: "bearer " + localStorage.getItem("ACCESS_TOKEN"), check: localStorage.getItem("username"),
                        Auth: getUuid(), 'Content-Type': 'application/json'},
                    success: function (data) {
                        if (data != "" && data.code == 200) {
                            alert("验证码发送成功");
                        } else {
                            alert(data.msg);
                            $("#checkCodeEmailOldBtn").text("获取验证码");
                        }
                    }
                });
            }
            countdown--;
        }
    }
}

function updateEmail(){
    if(userDetail.email != "" && userDetail.email != null && !validateCheckCodeEmailOld()){
        return false;
    }
    if(!validateEmail()){
        return false;
    }
    if(!validateCheckCodeEmail()) {
        return false;
    }
    var url = userDetail.email == "" ? "/user-center-oauth/oauthUser/addEmail" : "/user-center-oauth/oauthUser/updateEmail";
    var newEmail = $("#emailValue").val();
    var newEmailCaptcha = $("#checkCodeEmail").val();
    var oldEmailCaptcha = $("#checkCodeEmailOld").val();
    var data = {oldEmailCaptcha: oldEmailCaptcha, newEmail: newEmail, newEmailCaptcha: newEmailCaptcha};
    $.ajax({
        url: url,
        type: "post",
        dataType: "json",
        data: JSON.stringify(data),
        headers: {Authorization: "bearer " + localStorage.getItem("ACCESS_TOKEN"), check: localStorage.getItem("username"),
            Auth: getUuid(), 'Content-Type': 'application/json'},
        success: function (data) {
            if (data != "" && data.code == 200) {
                alert("操作成功");
                window.location.href = "personalCenter.html";
            } else {
                alert(data.msg);
            }
        }
    });
}

function updateNickname() {
    var name = $("#nicknameUpdate").val();
    var nickname = name.replace(/\s+/g, '')
    if(nickname == ""){
        alert("请输入昵称！");
        return;
    }
    if(nickname.length < 2 || nickname.length > 17) {
        alert("请输入2~16位字符！");
        return;
    }
    $request({
        url: '/user-center-oauth/oauthUser/updateNickName?nickname=' + encodeURI(nickname),
        method: 'post',
        headers: {"Content-Type": "application/json;charset=UTF-8"}
    }, function (res) {
        window.location.href = "personalCenter.html";
    });
}

function copyBidInfo(){
    if(userDetail.bid != ""){
        copyHandle(userDetail.bid);
    }
}

function downloadKeyStore(){
    if(userDetail.keyStore == ""){
        alert("私钥尚未备份，不能下载");
    }else{

    }
}

function updateBfPassword(){
    var bidSy = $("#bidSy").val();
    if(bidSy == ""){
        alert("请输入BID私钥");
        return false;
    }
    var bidPassword = $("#bidPassword").val();
    if(bidPassword == ""){
        alert("请输入私钥备份密码");
        return false;
    }
}